<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            width: 560px;
            height: 312px;
            background-color: rgb(249, 247, 248);
            border: 1px solid rgb(222, 222, 222);
            margin-left: 10px;
            margin-top: 10px;
        }

        .left {
            width: 456px;
            float: left;
        }

        .left img {
            width: 440px;
            height: 293px;
            margin-left: 8px;
            margin-top: 8px;
        }

        .right {
            height: 293px;
            margin-left: 8px;
            margin-top: 8px;
        }

        .right img {
            width: 95px;
            height: 62px;
            margin-top: 8px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="left"><img src="images/1.jpg" /></div>
        <div class="right">
            <img src="images/1.jpg" />
            <img src="images/2.jpg" />
            <img src="images/3.jpg" />
            <img src="images/4.jpg" />
        </div>
    </div>
</body>
<script>
    var img = document.querySelectorAll(".right>img");
    var timer;
    // 1、自动切换 图片
    var i = 1;
    img.forEach(function (item) {
        item.style.opacity = '0.5'
    })
    img[0].style.opacity = '1'
    play();
     function play(){
        timer = setInterval(function () {
        i++;
        if (i == 5) {
            i = 1;
        }
        console.log(i);
        document.querySelector(".left>img").src = `images/${i}.jpg`;
        // 透明度
        img.forEach(function (item, key) {
            // if (key == (i - 1)) {
            //     item.style.opacity = '1';
            // } else {
            //     item.style.opacity = '0.5';
            // }
            item.style.opacity = key == (i-1)?1:0.5;
        })
    }, 1500);
     }


    // 2、鼠标移入右侧的图片 左边切换到对应的图片  鼠标移出  以当前的图片 自动继续切换图片
    img.forEach(function (item, key) {
        item.addEventListener('mouseover', function (event) {
            clearInterval(timer);
            document.querySelector(".left>img").src = this.src
                i = key + 1;
            // 设置图片透明度
            img.forEach(function(item){
                item.style.opacity = "0.5"
            });
            this.style.opacity = '1';
        })
        item.addEventListener('mouseout', function () {
            play();
        })
    })

</script>